import md5 from "md5"
import sha256 from "js-sha256"
const signPw=function(str){
  return md5(sha256(str))
}
const getQueryData=function(url) {
  url=url.split("?")[1]
  let result = {};
  if(url){
    let str = url;
    let strs = str.split("&");
    for (let i = 0; i < strs.length; i++) {
      result[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
    }
  }
  return result;
}
const isArr= function(obj) {
  return Object.prototype.toString.call(obj) == '[object Array]'
}
const isObj= function(obj) {
  return Object.prototype.toString.call(obj) == '[object Object]';
}
const trim=function(str){ //删除左右两端的空格
  return str.replace(/(^\s*)|(\s*$)/g, "");
}

const sortASCII=function(obj){
  let sortArr = Object.keys(obj).sort();
  var sortObj = {};
  for (var i in sortArr) {
    if (obj[sortArr[i]]||obj[sortArr[i]]===0) {
      sortObj[sortArr[i]] = obj[sortArr[i]];
      if(isArr(obj[sortArr[i]])){
        obj[sortArr[i]].forEach((item,index)=>{
          if(isObj(item)){
            obj[sortArr[i]].splice(index,1,sortASCII(item))
          }else {
            obj[sortArr[i]].sort()
          }
        })
      }else if(isObj( obj[sortArr[i]])){
        obj[sortArr[i]]=sortASCII( obj[sortArr[i]])
      }
    }
  }
  return sortObj;
}
const filteremoji=function(content) {
  var ranges = [
    '\ud83c[\udf00-\udfff]',
    '\ud83d[\udc00-\ude4f]',
    '\ud83d[\ude80-\udeff]'
  ];
  let emojireg = content.replace(new RegExp(ranges.join('|'), 'g'), '');
  return emojireg;
}
const unique=function(arr) {
  return Array.from(new Set(arr))
}
const makeSign = function(obj){
  if (!obj) { console.log('需要加密的数组对象为空'); return}
  let str = ''
  let secret = 'key=a8953039d236276bc3a8344009f4a485'  //开发
  // let secret = 'key=86ca3e71bb6a3877f13265c39da936c2'  //线上
  // if (!secret) { console.log('密钥未获取') }
  //生成key升序数组
  let arr = Object.keys(obj)
  arr.sort()

  for (let i in arr) {
    if(typeof(obj[arr[i]])=='string') {
      obj[arr[i]]=trim(obj[arr[i]])
      // obj[arr[i]]=filteremoji(obj[arr[i]]) //移动端开启
    }
    if (obj[arr[i]]||obj[arr[i]]===0) {

      let signstr=""
      if(isArr(obj[arr[i]])){
        obj[arr[i]].forEach((item,index)=>{
          if(isObj(item)){
            obj[arr[i]].splice(index,1,sortASCII(item))
          }else {
            obj[arr[i]].sort()
          }
        })
        signstr=JSON.stringify(obj[arr[i]])
      }else if(isObj(obj[arr[i]])){
        obj[arr[i]]=sortASCII(obj[arr[i]])
      }else {
        signstr=obj[arr[i]]
      }
      str += arr[i] + '=' + signstr + '&'
    }
  }
//        var encrypted = this.md5(encodeURIComponent(str + secret, 'UTF-8')).toUpperCase()
  let encrypted = md5(str + secret, 'UTF-8').toUpperCase()
  // console.log(str + secret)
  return encrypted

}
const inputBlur =function(){
  setTimeout(function() {
    var scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0;
    window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  }, 100)
}
export default {
  makeSign: makeSign,
  getQueryData: getQueryData,
  signPw: signPw,
  inputBlur: inputBlur,
  unique: unique,
}

